//
//  SWBooksFetchReq.swift
//  ChildStory
//
//  Created by linhan on 2017/1/6.
//  Copyright © 2017年 Aiya. All rights reserved.
//

import Foundation

class SWBookListFetchReq: SWBaseRequest
{
    override init()
    {
        super.init()
        api = SWRequestAPI.BookListFetch
        options.authorize = false
        options.resultCodeCheck = false
    }
    
    override func JSONToModel(_ json:JSON, JSONString:String, JSONData:Data) -> AnyObject?
    {
        return Mapper<SWBookListFetchRsp>().map(JSONString: JSONString)
    }
    
    override func load()
    {
        DispatchQueue.global(qos: .default).async {
            if let url = Bundle.main.url(forResource: "book_templates", withExtension: "json"),
                let data = try? Data(contentsOf: url)
            {
                self.didFinishLoading(data, cache: true)
            }
        }
    }
}

class SWBookListFetchRsp: NSObject,Mappable
{
    var code:String?
    var books:[SWBookTemplateRsp]?
    
    required init?(map: Map) { }
    
    // Mappable
    func mapping(map: Map) {
        code       <- map["Statu"]
        books      <- map["BaseModle"]
    }
}

class SWBookTemplateRsp: NSObject,Mappable
{
    var id:String?
    var title:String?
    var version:Int?
    var pages:[SWPageTemplateRsp]?
    var category:Int?
    
    required init?(map: Map) { }
    
    // Mappable
    func mapping(map: Map) {
        id              <- map["id"]
        title           <- map["name"]
        version         <- map["version"]
        category            <- map["category"]
        pages           <- map["pages"]
    }
}

class SWPageTemplateRsp: NSObject,Mappable
{
    var version:Int?
    var pageID:String?
    var eventID:String?
    var title:String?
    var previewBgURL:String?
    var largeBgURL:String?
    var photos:[SWPhotoTemplateRsp]?
    var texts:[SWTextTemplateRsp]?
    
    required init?(map: Map) { }
    
    // Mappable
    func mapping(map: Map) {
        version         <- map["version"]
        pageID          <- map["pageID"]
        eventID           <- map["eventID"]
        title           <- map["title"]
        previewBgURL           <- map["previewBgURL"]
        largeBgURL           <- map["largeBgURL"]
        photos           <- map["photos"]
        texts           <- map["texts"]
    }
}

class SWPhotoTemplateRsp: NSObject,Mappable
{
    var xPercent:Double?
    var yPercent:Double?
    var widthPercent:Double?
    var heightPercent:Double?
    
    required init?(map: Map) { }
    
    // Mappable
    func mapping(map: Map) {
        xPercent          <- map["xPercent"]
        yPercent          <- map["yPercent"]
        widthPercent      <- map["widthPercent"]
        heightPercent     <- map["heightPercent"]
    }
}

class SWTextTemplateRsp: NSObject,Mappable
{
    var defaultText:String?
    var textColor:String?
    var fontSize:Int?
    var xPercent:Double?
    var yPercent:Double?
    var widthPercent:Double?
    var heightPercent:Double?
    
    required init?(map: Map) { }
    
    // Mappable
    func mapping(map: Map) {
        defaultText       <- map["defaultText"]
        textColor         <- map["textColor"]
        fontSize          <- map["fontSize"]
        xPercent          <- map["xPercent"]
        yPercent          <- map["yPercent"]
        widthPercent      <- map["widthPercent"]
        heightPercent     <- map["heightPercent"]
    }
}


